<template>
    <div class="login">
        <h2>登录到木也</h2>
        <div class="email">
            <span>邮箱地址</span>
            <div class="input"><input type="text" placeholder="请输入邮箱地址" v-model="email.val"/></div>
            <div v-if="email.error" class="message">{{email.message}}</div>
        </div>
        <div class="email">
            <span>密码</span>
            <div class="input"><input type="text" placeholder="请输入密码" v-model="password.val"/></div>
            <div v-if="password.error" class="message">{{password.message}}</div>
        </div>
        <a href="javascript:;">还没有账户?去注册一个新的吧</a>
        <div class="login-btn" @click="loginBtn">登录</div>
    </div>
</template>

<script lang="ts">
import { defineComponent, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';
let emailReg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.]){1,2}[A-Za-z\d]{2,5}$/g;
export default defineComponent({
    name: 'login',
    setup() {
        const router = useRouter();
        const store = useStore();
        const email = reactive({
            val: '',
            error: false,
            message: ''
        });
        const password = reactive({
            val: '',
            error: false,
            message: ''
        });
        const loginBtn = () => {
            if(!emailReg.test(email.val) || password.val.length < 6) {
                email.error = true;
                email.message = '邮箱格式错误';
                password.error = true;
                password.message = '密码至少大于6位数';
                return;
            }
            email.error = false;
            email.message = '';
            password.error = false;
            password.message = '';
            store.commit('login', {isLogin: true, name: 'Leo'});
            router.push('/');
        }
        return {
            email,
            password,
            loginBtn
        }
    }
})
</script>

<style lang="less" scoped>
.login {
    width: 330px;
    margin: auto;
    padding: 16px;
    h2 {
        color: #212529;
        font-size: 20px;
        margin: 40px auto 20px;
        text-align: center;
    }
    .email {
        position: relative;
        span {
            color: #212529;
            font-size: 16px;
            margin-bottom: 10px;
        }
        .input {
            margin-bottom: 20px;
            margin-top: 10px;
            padding-left: 10px;
            box-sizing: border-box;
            height: 38px;
            border: 1px solid rgba(0,0,0,.2);
            input {
                outline: none;
                border: 0px;
                width: 100%;
                height: 100%;
            }
        }
        .message {
            position: absolute;
            bottom: -16px;
            left: 0px;
            color: #f60;
            font-size: 12px;
        }
    }
    .login-btn {
        height: 40px;
        background-color: #0d6efd;
        border-radius: 5px;
        text-align: center;
        line-height: 40px;
        color: #fff;
        cursor: pointer;
        margin-top: 10px;
    }
}
</style>